/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { X2DBeta } = require("../../../engine/graphics/X2DInterface");
const { Scr } = require("../../../engine/XDefine");
const { XTools } = require("../../../engine/XTools");

class CustomBase {
    constructor(type, name) {
        this.type = type;
        this.name = name;
        this.x = XTools.RandInt(Scr.logicw / 2 - 100, Scr.logicw / 2 + 100);
        this.y = XTools.RandInt(Scr.logich / 2 - 100, Scr.logich / 2 + 100);
        this.w = 200;
        this.h = 32;
        this.draging = false;
        this.token = XTools.GenToken(16);
        this.lastClickTime = 0;
    }
    onDrawExtend(dx, dy) {
        return 0;
    }
    onDraw(offx = 0, offy = 0, locked = false) {
        let x = this.x + offx;
        let y = this.y + offy;
        let neww = 200;
        X2DBeta.fillRect(x, y, this.w, 16, 0xff303030);
        if (this.type == "node") {
            X2DBeta.drawText("自定义节点", 12, x + this.w / 2, y + 8, 1, 1, 0, -2, -2, 0xff7bed9f);
        } else {
            X2DBeta.drawText("自定义消息", 12, x + this.w / 2, y + 8, 1, 1, 0, -2, -2, 0xfff1c40f);
        }

        // X2DBeta.drawText("topic" + (this.isSubscribed ? "(" + this.msgReceived + ")" : ""), 12, x + this.w / 2, y + 8, 1, 1, 0, -2, -2, 0xfff1c40f);
        y += 16;

        X2DBeta.fillRect(x, y, this.w, this.h - 16, 0xff404040);
        neww = Math.max(neww, X2DBeta.drawText(this.name, 12, x + 4, y + 8, 1, 1, 0, -1, -2, 0xffe0e0e0) + 8);
        y += 16;

        y += this.onDrawExtend(x, y);

        if (neww != this.w) {
            this.w = neww;
        }
        let newh = y - offy - this.y;
        if (newh != this.h) {
            this.h = newh;
        }

        if (locked) {
            X2DBeta.drawRect(this.x + offx, this.y + offy, this.w, this.h, 0xff00ff00);
        }
    }
    onTouch(msg, x, y) {
        if (this.draging) {
            if (msg == 2) {
                let dx = x - this.draging.x;
                let dy = y - this.draging.y;
                this.x += dx;
                this.y += dy;
                this.draging.x = x;
                this.draging.y = y;
                const { ProjectConfig } = require("../ProjectConfig");
                ProjectConfig.getInstance().setCustomAttr();
                return true;
            }
            this.draging = false;
            return true;
        }
        if (XTools.InRect(x, y, this.x, this.y, this.w, this.h)) {
            if (msg == 1) {
                const { CustomManager } = require("./CustomManager");
                CustomManager.getInstance().resetEditing(this);
                if (XTools.TIME_MS - this.lastClickTime < 300) {
                    CustomManager.getInstance().onDoubleClick(this);
                    this.lastClickTime = 0;
                } else {
                    this.lastClickTime = XTools.TIME_MS;
                    this.draging = {
                        x: x,
                        y: y,
                    }
                }
            }
            return true;
        }
        return false;
    }
}

module.exports = {
    CustomBase,
}